﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KillBill.Business.Entities;
using KillBill.Business.Entities.ListEntities;
using KillBill.Business.Modules.Suppliers;
using KillBill.Services.DTO;
using KillBill.Services.Modules.Suppliers.IServices;
using KillBill.Services.Modules.Suppliers.SecurityServiceReference;

namespace KillBill.Services.Modules.Suppliers
{
    public class SuppliersService : ISuppliersService
    {
        private readonly SuppliersManager _suppliersManager;
        private readonly SecurityServiceClient _securityService;

        public SuppliersService()
        {
            _suppliersManager = new SuppliersManager();
            _securityService = new SecurityServiceClient();
        }

        #region ISuppliersService Members

        public ProviderList GetProviders(string sessionId)
        {
            var list = new ProviderList();
            list.DTO = _securityService.IsAuthorized(sessionId, new string[] { "Administrador" });
            if (list.Success)
                list = _suppliersManager.GetProviders();
            return list;
        }

        public ProviderList FindProviders(string value, string sessionId)
        {
            var list = new ProviderList();
            list.DTO = _securityService.IsAuthorized(sessionId, new string[] { "Administrador" });
            if (list.Success)
                list = _suppliersManager.FindProviders(value);
            return list;
        }

        public Provider AddProvider(Provider provider, string sessionId)
        {
            KillBillDTO sessionDTO = _securityService.IsAuthorized(sessionId, new string[] { "Administrador", "Bodeguero" });
            var outDTO = new Provider();
            if (!sessionDTO.Success)
            {
                outDTO.Success = false;
                outDTO.Messages = sessionDTO.Messages;

            }
            else
                outDTO = _suppliersManager.AddProvider(provider);
            return outDTO;
        }

        public Provider UpdateProvider(Provider provider, string sessionId)
        {
            KillBillDTO sessionDTO = _securityService.IsAuthorized(sessionId, new string[] { "Administrador", "Bodeguero" });
            var outDTO = new Provider();
            if (!sessionDTO.Success)
            {
                outDTO.Success = false;
                outDTO.Messages = sessionDTO.Messages;
            }
            else
                outDTO = _suppliersManager.UpdateProvider(provider);
            return outDTO;
        }

        public KillBillDTO DeteleProvider(Provider provider, string sessionId)
        {
            KillBillDTO sessionDTO = _securityService.IsAuthorized(sessionId, new string[] { "Administrador", "Bodeguero" });
            var outDTO = new KillBillDTO();
            if (!sessionDTO.Success)
            {
                outDTO.Success = false;
                outDTO.Messages = sessionDTO.Messages;
                return outDTO;
            }
            else
                //outDTO = _suppliersManager.DeteleProvider(provider);
                return outDTO;
        }

        #endregion
    }

}
